<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class LoginController extends Zend_Controller_Action 
{
	/**
	 * The default action - show the home page
	 */
    public function indexAction() 
    {
        $this->view->form = new FormLogin(array('action' => $this->getFrontController()->getBaseUrl().'users/login'));
    }
    
    public function validateAction() {
    	
		$params = new Zend_Filter_Input ( null, null, $this->getRequest ()->getParams () );
				
		$dbAdapter = Zend_Db_Table_Abstract::getDefaultAdapter ();
		
		$authAdapter = new Zend_Auth_Adapter_DbTable ( $dbAdapter, "users", "login", "pass" );
		$authAdapter->setIdentity ( $params->login );
		$authAdapter->setCredential ( sha1( $params->senha ) );
		$result = Zend_Auth::getInstance ()->authenticate ( $authAdapter );
		
		$config = Zend_Registry::get ( 'config' );
		
		if ($result->isValid ()) {
			Zend_Auth::getInstance ()->setStorage ( new Zend_Auth_Storage_Session ( $config->session->namespace ) );
			$usuario = $authAdapter->getResultRowObject ( null, "senha" );
			Zend_Auth::getInstance ()->getStorage ()->write ( $usuario );
			
			$this->_redirect ( './' );
		
		}
    }
    
}
